﻿using System;
using System.Collections.Generic;
using System.Text;
using CNShapes;
using BobcatMultiTouch;
using System.Drawing;
using BobcatUtility;

namespace CNPageEditor
{
    /// <summary>
    /// This action lets you drag the whole picture arouond with the mouse (or other
    /// input device).
    /// </summary>
    public class Action_DragPicture : Action
    {
        #region Action methods

        /// <summary>
        /// Called to set up the action.
        /// </summary>
        public override void initialize()
        {
            m_OriginBeforeDrag = Utility.copy(m_PageViewerControl.Origin);
            m_OriginAfterDrag = Utility.copy(m_PageViewerControl.Origin);
        }

		/// <summary>
		/// Called when the action becomes inactive.
		/// </summary>
		public override void deactivate()
		{
		}

		/// <summary>
        /// Returns the string description of this action.
        /// </summary>
        public override string getDescription()
        {
            return "Drag picture";
        }

        /// <summary>
        /// We do not support undo for this action (as it just affects the view, not the picture itself).
        /// </summary>
        public override bool supportsUndo()
        {
            return false;
        }

        /// <summary>
        /// This action is repeatable.
        /// </summary>
        public override bool isRepeatable()
        {
            return true;
        }

        /// <summary>
        /// Not supported for this action
        /// </summary>
        public override void undo()
        {
        }

        /// <summary>
        /// Not supported for this action
        /// </summary>
        public override void redo()
        {
        }

        /// <summary>
        /// Called when we get a touch-down event.
        /// </summary>
        public override void onTouchDown(TouchArgs touchArgs)
        {
            // We store the initial touch position (in client-coords)...
            m_OriginBeforeDrag = Utility.copy(m_PageViewerControl.Origin);
            m_DragStart = Utility.copy(touchArgs.ClientPosition);
        }

        /// <summary>
        /// Called when we get a touch-move event. 
        /// </summary>
        public override void onTouchMove(TouchArgs touchArgs)
        {
            // We find the new position for the screen and move it. 
            
            // To do this wee get the new touch position and see how far we've moved
            // for this zoom factor...
            float fXOffset = (touchArgs.ClientPosition.X - m_DragStart.X) / m_PageViewerControl.ZoomFactor;
            float fYOffset = (touchArgs.ClientPosition.Y - m_DragStart.Y) / m_PageViewerControl.ZoomFactor;

            // We work out the new view origin and set it...
            m_OriginAfterDrag.X = m_OriginBeforeDrag.X - fXOffset;
            m_OriginAfterDrag.Y = m_OriginBeforeDrag.Y - fYOffset;
            m_PageViewerControl.Origin = m_OriginAfterDrag;
        }

        /// <summary>
        /// Called when we receive a touch-up event.
        /// </summary>
        public override void onTouchUp(TouchArgs touchArgs)
        {
            // The action has completed...
            raiseActionCompletedEvent();
        }

        #endregion

        #region Private data

        // The point that the drag started from...
        private Point m_DragStart = new Point();

        // We store the original view origin so that we can undo the action, and the new one
        // so that we can redo it...
        private PointF m_OriginBeforeDrag = new PointF();
        private PointF m_OriginAfterDrag = new PointF();

        #endregion



    }
}
